/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhuangfei.ohos_timetableview.slice;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.base.BaseAbilitySlice;
import com.zhuangfei.ohos_timetableview.custom.CustomOperater;
import com.zhuangfei.ohos_timetableview.dialog.ToastViewDialog;
import com.zhuangfei.ohos_timetableview.model.MySubject;
import com.zhuangfei.ohos_timetableview.model.SubjectRepertory;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.listener.OnDateBuildAapter;
import com.zhuangfei.timetable.listener.OnSpaceItemClickAdapter;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;

import java.util.List;

/**
 * CustomWidthSlice
 *
 * @since 2021-03-29
 */
public class CustomWidthSlice extends BaseAbilitySlice {
    /**
     * 控件
     */
    TimetableView mTimetableView;
    CustomOperater operater;
    List<MySubject> mySubjects;
    Intent intents;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_custom_width);
        mySubjects = SubjectRepertory.loadDefaultSubjects2();
        mySubjects.addAll(SubjectRepertory.loadDefaultSubjects());
        intents = intent;
        initViews();
    }

    private void initViews() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        operater = new CustomOperater();
        operater.setWidthWeights(new float[]{1, 3, 1, 1, 1, 1, 1});
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .maxSlideItem(10)
                .monthWidthDp(30)
                .operater(operater)
                .callback(new ISchedule.OnFlaglayoutClickListener() {
                    @Override
                    public void onFlaglayoutClick(int day, int start) {
                        CustomWidthSlice.this.getUITaskDispatcher().asyncDispatch(() -> {
                            mTimetableView.hideFlaglayout();
                        });
                        ToastViewDialog.toast(CustomWidthSlice.this, "点击了旗标:周" + (day + 1) + ",第" + start + "节");
                    }
                })
                .callback(new OnDateBuildAapter() {
                    @Override
                    public Component[] getDateViews(LayoutScatter scatter, float monthWidth,
                                                    float perWidth, int height) {
                        Component[] views = new Component[8];
                        views[0] = onBuildMonthLayout(scatter, (int) monthWidth, height);
                        float[] weights = operater.getWeights();
                        double sum = 0;
                        for (int i = 0; i < weights.length; i++) {
                            sum += weights[i];
                        }
                        for (int i = 1; i < 8; i++) {
                            double rato = weights[i - 1] / sum;
                            views[i] = onBuildDayLayout(scatter, i, (int) (7 * perWidth * rato), height);
                        }
                        return views;
                    }
                })
                .callback(new OnSpaceItemClickAdapter() {
                    @Override
                    public void onSpaceItemClick(int day, int start) {
                        /**
                         * day:从0开始，start：从1开始
                         */
                        if (flagLayout == null) {
                            return;
                        }
                        float[] weights = operater.getWeights();
                        double sum = 0;
                        for (int i = 0; i < weights.length; i++) {
                            sum += weights[i];
                        }
                        int newItemWidth = (int) (super.itemWidth * 7 * weights[day] / sum);
                        double newMarLeft = 0;
                        for (int i = 0; i < day; i++) {
                            newMarLeft += super.itemWidth * 7 * weights[i] / sum;
                        }
                        StackLayout.LayoutConfig lp = new StackLayout.LayoutConfig(
                                newItemWidth - marLeft * 2, itemHeight);
                        lp.setMargins(monthWidth + (int) newMarLeft + marLeft, (start - 1) * (
                                itemHeight + marTop) + marTop, 0, 0);
                        flagLayout.setLayoutConfig(lp);
                    }
                })
                .showView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
